package xworker.dataObject.data;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * 数据选择器。
 */
public abstract class DataSelector<T> {
    protected List<T> allItems;
    final List<T> selectItems = new ArrayList<>();
    final List<DataSelectorListener<T>> listeners = new ArrayList<>();

    public DataSelector(){
    }

    public int getSelectedCount(){
        return selectItems.size();
    }

    public T getSelectedItem(){
        if(selectItems.size() > 0){
            return selectItems.get(0);
        }else{
            return null;
        }
    }

    /**
     * 返回选中的条目列表。
     */
    public List<T> getSelectedItems() {
        return selectItems;
    }

    public void addListener(DataSelectorListener<T> listener){
        if(!listeners.contains(listener)){
            listeners.add(listener);
        }
    }

    public void removeListener(DataSelectorListener<T> listener){
        listeners.remove(listener);
    }

    public void fireSelectionChanged(){
        for(DataSelectorListener<T> listener : listeners){
            listener.selectionChanged(this);
        }
    }

    /**
     * 返回所有的条目列表，如果不存在返回空的列表。
     */
    public List<T> getAllItems() {
        return allItems != null ? allItems : Collections.emptyList();
    }

    public void setAllItems(List<T> allItems) {
        this.allItems = allItems;
    }

    public void clearSelectedItems(){
        selectItems.clear();
    }

    public void setSelectedItem(T t){
        selectItems.clear();

        selectItems.add(t);
    }

    public void setSelectedItems(Collection<T> items){
        selectItems.clear();

        selectItems.addAll(items);
    }

    public void addSelectedItem(T t){
        selectItems.add(t);
    }

    public void addSelectedItems(Collection<T> items){
        selectItems.addAll(items);
    }

    public void removeSelectedItem(T t){
        selectItems.remove(t);
    }

    public void removeSelectedItemList(Collection<T> ts){
        selectItems.removeAll(ts);
    }

    public abstract void clearSelection();

    public abstract void removeSelection(T t);

    public abstract void removeSelectionAll(Collection<T>  ts);

    public abstract void addSelection(T t);

    public abstract void addSelectionAll(Collection<T> ts);

    public abstract void setSelectionAll(List<T> ts);

    public abstract void setSelection(T t);

    /**
     * 通过数据的标识设置选择，默认未实现。
     */
    public abstract void setSelectionByIds(Object ... ids);

    /**
     * 返回选中的条目的标识泪飙，默认返回空的List。
     */
    public abstract List<Object> getSelectedItemsIdList();

    public Object getSelectedItemId(){
        List<Object> ids = getSelectedItemsIdList();
        if(ids != null && ids.size() > 0){
            return ids.get(0);
        }else{
            return null;
        }
    }

    public String getSelectedItemsIdString(){
        StringBuilder sb = new StringBuilder();
        List<Object> ids = getSelectedItemsIdList();
        if(ids != null){
            for(Object obj : ids){
                if(sb.length() > 0){
                    sb.append(",");
                }
                sb.append(obj);
            }
        }

        return sb.toString();
    }

    public String getSelectedItemIdString(){
        List<Object> ids = getSelectedItemsIdList();
        if(ids != null && ids.size() > 0){
            return String.valueOf(ids.get(0));
        }

        return null;
    }
}
